package com.xuyh.pdf.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.xuyh.pdf.utils.ttjspider.*;

import java.util.Date;
import java.util.List;

/**
 * @author xuyh0817
 * @version 1.0.0
 * @email xuyh0817@foxmail.com
 * @date 2024/9/23 10:59
 * @doc
 * @website
 * @description todo
 */
public class SpideHtml {
    public static void main(String[] args) throws Exception {
//        spider("http://xjzd.stats.gov.cn/jdhy/zcfg/tjfg/",3L,12L,"新疆调查总队","3", DateUtil.date().toJdkDate());
        spider("http://10.165.1.1/wjtz/tzgg/", 71L, 3L, 12L, "新疆调查总队", "3", DateUtil.date().toJdkDate(), 5);
    }


    private static void spider(String url, Long categoryId, Long tenantId, Long deptId, String deptName, String creator, Date createDate, Integer pageTotal) throws Exception {
        SpiderArticle spiderArticle = new SpiderArticle(url, pageTotal);
        List<Article> articles = spiderArticle.spiderHtml();

        InsertArticle insertArticle = new InsertArticle();
        InsertAttachment insertAttachment = new InsertAttachment();

        for (Article article : articles) {
            List<Attachment> images = article.getImages();
            List<Attachment> attachments = article.getAttachments();
            for (Attachment image : images) {
                image.setTenantId(tenantId);
                image.setCreator(creator);
                image.setCreateTime(createDate);
                image.setUpdater(creator);
                image.setUpdateTime(createDate);
            }

            for (Attachment attachment : attachments) {
                attachment.setTenantId(tenantId);
                attachment.setCreator(creator);
                attachment.setCreateTime(createDate);
                attachment.setUpdater(creator);
                attachment.setUpdateTime(createDate);
            }

            insertAttachment.insert(images);
            insertAttachment.insert(attachments);

            article.setTenantId(tenantId);
            article.setDeptId(deptId);
            article.setCategoryId(categoryId);
            article.setDeptName(deptName);
            article.setCreator(creator);
            article.setCreateTime(createDate);
            article.setUpdater(creator);
            article.setUpdateTime(createDate);
            insertArticle.insert(article);
        }
    }

}
